package com.example.xn.snakegame;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioRouting;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    long exitTime;
    public static SnakeView mSnakeView;
     orication orication;
    private ImageButton btn_up;
    private ImageButton btn_down;
    private ImageButton btn_left;
    private ImageButton btn_right;
    public static  ImageView btn_pause;
    public static TextView time_text;
    public static TextView score;
    public static FrameLayout frameLayout;
    private TextView text;
   public static  int time;
    private static String ICICLE_KEY = "snake-view";
    public  static LinearLayout layout;
   public static  MainActivity main;
    public static  TextView timeText;
    public static FrameLayout.LayoutParams params;
    public MygameApplication mygameApplication;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mygameApplication= (MygameApplication) getApplication();
        main=this;
        time=getIntent().getIntExtra("item",30);
        mSnakeView= (SnakeView) findViewById(R.id.snake);
        text= (TextView) findViewById(R.id.text);
        mSnakeView.setTextView(text);
        btn_pause= (ImageView) findViewById(R.id.btn_pause);
        btn_up= (ImageButton) findViewById(R.id.btn_up);
        btn_down= (ImageButton) findViewById(R.id.btn_down);
        btn_left= (ImageButton) findViewById(R.id.btn_left);
        btn_right= (ImageButton) findViewById(R.id.btn_right);
        time_text= (TextView) findViewById(R.id.time);
        score= (TextView) findViewById(R.id.score);
        layout= (LinearLayout) findViewById(R.id.layout_show);
        frameLayout= (FrameLayout) findViewById(R.id.activity_main);
        time_text.setText(time+"");
        setOrication(mSnakeView);
        //设置倒计时
        timeText=new TextView(this);
         params=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
        timeText.setText(MainActivity.time+"");
       // timeText.setLayoutParams(params);
         frameLayout.addView(timeText,params);
        //params.setMargins(mSnakeView.getWidth()/2,30,0,0);
          timeText.setTextSize(20);
         timeText.setTextColor(getResources().getColor(R.color.colorAccent));
        timeText.setVisibility(View.INVISIBLE);
        //设置结束
        btn_right.setOnClickListener(this);
        btn_down.setOnClickListener(this);
        btn_up.setOnClickListener(this);
        btn_left.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
        if (savedInstanceState == null) {
            // We were just launched -- set up a new game

          // mSnakeView.update();
            mSnakeView.invalidate();
            mSnakeView.setMode(SnakeView.READY);

        } else {
            // We are being restored
            Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
            if (map != null) {

                mSnakeView.restoreState(map);
            } else {
                mSnakeView.setMode(SnakeView.PAUSE);
            }
        }
    }

    @Override
    protected void onPause() {
        Toast.makeText(this,"onpause",Toast.LENGTH_SHORT).show();
        super.onPause();
        mSnakeView.setMode(SnakeView.PAUSE);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
       //outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(MainActivity.this, "再按一次退出软件", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                mSnakeView.setMode(SnakeView.PAUSE);
                finish();
            }
            return true;
        }


        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_up:
                orication.orderorication(1);
                break;
            case R.id.btn_down:
                orication.orderorication(2);
                break;
            case R.id.btn_left:
                orication.orderorication(4);
                break;
            case R.id.btn_right:
                orication.orderorication(3);
                break;
            case R.id.btn_pause:
                orication.orderorication(0);

        }
    }


    interface orication{
        void orderorication(int direaction);
    }
    void setOrication(orication orication)
    {
        this.orication=orication;
    }
    @Override
    protected void onStop() {
        Toast.makeText(this,"onstop",Toast.LENGTH_SHORT).show();
        mygameApplication.mediaPlayer.pause();
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        Intent intent=new Intent(this,GnamemusicService.class);
        stopService(intent);


        super.onDestroy();
    }

    @Override
    protected void onResume() {
//        if(!mygameApplication.mediaPlayer.isPlaying())
//        mygameApplication.mediaPlayer.start();

        super.onResume();
    }
}
